package com.example.exam5;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class exam5 extends AppCompatActivity implements View.OnClickListener, DialogInterface.OnClickListener {
    //取消按钮的常量值
    public static final int BTN_NEG = -2;
    //确认按钮的常量值
    private static final int BTN_POS = -1;
    TextView tvRegister;
    Button btnOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam5);
        tvRegister = findViewById(R.id.tv_register);
        btnOut = findViewById(R.id.btn_out);
        //注册单击监听器
        tvRegister.setOnClickListener(this);
        btnOut.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        switch (v.getId()) {
            case R.id.tv_register:
                //获取视图
                View view = LayoutInflater.from(this).inflate(R.layout.form_register, null, false);
                builder.setView(view)
                        .create()
                        .show();
                break;
            case R.id.btn_out:
                //构造dialog
                builder.setIcon(R.drawable.alert)
                        .setTitle("退出？")
                        .setMessage("确定要退出吗？")
                        .setPositiveButton("确定", this)
                        .setNegativeButton("取消", this)
                        .create()
                        .show();
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case BTN_POS:
                //结束当前activity
                finish();
                break;
            case BTN_NEG:
                //关闭dialog
                dialog.cancel();
                break;
            default:
                break;
        }
    }
}
